#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#define NUMBYTES (26 * 1024*1024)
#define STEPSIZE 4096
#define PRINTTHREASHOLD (15 * 1024*1024)
#define PRINTSTEPSIZE (1024*1024)

struct vmstats_OSL4 {
    unsigned long long active_pages,
        inactive_pages,
        active_referenced_pages,
        inactive_referenced_pages,
        pages_active2inactive,
        evicted_pages,
        allocated_pages;
};

void PrintMemStats ();

// We need to set this stuff up to compile for UML
#include "linux-2.6.32.8/arch/x86/include/asm/unistd.h"
#define _NR_sys_print_mem_stats 338

int main(int argc, char **argv) {
//	int s[1000000];
//	int i;
//	for (i=0; i<1000000; i++) {
//		s[i] = 2;
//	}

	printf ("Startup\n");
	PrintMemStats ();

	int i;
	for (i=0; i<NUMBYTES / STEPSIZE; i++) {
	 	char *lots_of_bytes = malloc (STEPSIZE);
	 	if (lots_of_bytes == NULL) {
	 		fprintf (stderr, "You asked for too much memory!\n");
			return 1;
		}

		int j;
		for (j=0; j<STEPSIZE; j++) {
			lots_of_bytes[j] = 2;
		}

		if (i > PRINTTHREASHOLD / STEPSIZE && (i % PRINTSTEPSIZE / STEPSIZE) == 0) {
			printf ("Step %i\n", i);
			PrintMemStats ();
		}
	}

	sleep (30);
	printf ("After sleeping\n");
	PrintMemStats ();

    return 0;
}

void PrintMemStats () {
	struct vmstats_OSL4 stats_struct;
    
	syscall(_NR_sys_print_mem_stats, &stats_struct);

	printf ("-----\n");
	printf ("active: %llu\n", stats_struct.active_pages); 
     printf ("inactive: %llu\n", stats_struct.inactive_pages); 
     printf ("active_referenced: %llu\n", stats_struct.active_referenced_pages); 
     printf ("inactive_referenced: %llu\n", stats_struct.inactive_referenced_pages); 
     printf ("active2in: %llu\n", stats_struct.pages_active2inactive); 
     printf ("evicted: %llu\n", stats_struct.evicted_pages); 
     printf ("alloc: %llu\n", stats_struct.allocated_pages); 
	printf ("-----\n");

}

